package com.zhuiyun.project.api.plan.fitnessAction.service.impl;

import com.zhuiyun.project.api.plan.fitnessAction.entity.FitnessAction;
import com.zhuiyun.project.api.plan.fitnessAction.mapper.FitnessActionMapper;
import com.zhuiyun.project.api.plan.fitnessAction.model.FitnessActionModel;
import com.zhuiyun.project.api.plan.fitnessAction.model.FitnessActionsModel;
import com.zhuiyun.project.api.plan.fitnessAction.service.FitnessActionService;
import com.zhuiyun.project.api.plan.fitnessPlanActionCount.mapper.FitnessPlanActionCountMapper;
import com.zhuiyun.project.api.plan.fitnessPlanActionGroup.mapper.FitnessPlanActionGroupMapper;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.UploadFileUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName FitnessActionServiceImpl
 * @Description TODO
 * @Author ys
 * @Date 2023/11/2 9:01
 **/
@Service
public class FitnessActionServiceImpl implements FitnessActionService {
    @Resource
    private FitnessActionMapper fitnessActionMapper;
    @Resource
    private FitnessPlanActionGroupMapper fitnessPlanActionGroupMapper;
    /**
     * @Author 杨珅
     * @Description todo 分页查询健身动作
     * @Date 2023/11/13 8:43
     * @Return * @param null
     */
    @Override
    public PageResult selectAll(PageRequest pageRequest, String fitnessActionName, String venueUniqueIdentifier, Integer fitnessPurposeId) {
        MybatisPageHelper.startPage(pageRequest);// 使用MybatisPageHelper实现分页功能，设置分页信息
        List<FitnessActionModel> ActionList = fitnessActionMapper.selectAll(pageRequest, fitnessActionName,venueUniqueIdentifier,fitnessPurposeId);// 调用ArticleReviewMapper的getArticle方法查询文章列表\
        return MybatisPageHelper.getPageResult(ActionList);
    }
    /**
     * @Author 杨珅
     * @Description todo 根据健身部位名和目的查询动作
     * @Date 2023/11/2 9:28
     * @Return  * @param null
     */
    @Override
    public CommonResult queryAll(String venueUniqueIdentifier,Integer fitnessPurposeId,String fitnessTypeName) {
        return CommonResult.ok(fitnessActionMapper.queryAll(venueUniqueIdentifier,fitnessPurposeId,fitnessTypeName));
    }
    /**
     * @Author 杨珅
     * @Description todo 根据健身部位id和目的查询动作
     * @Date 2023/11/2 9:28
     * @Return  * @param null
     */
    @Override
    public CommonResult queryByType(String venueUniqueIdentifier, Integer fitnessPurposeId) {
        return CommonResult.ok(fitnessActionMapper.queryByType(venueUniqueIdentifier,fitnessPurposeId));
    }
    /**
     * @Author 杨珅
     * @Description todo 根据健身部位id和目的查询动作（分页版）
     * @Date 2023/11/2 9:28
     * @Return  * @param null
     */
    @Override
    public PageResult queryByTypes(PageRequest pageRequest, String venueUniqueIdentifier, Integer fitnessPurposeId, Integer fitnessTypeId) {
        MybatisPageHelper.startPage(pageRequest);// 使用MybatisPageHelper实现分页功能，设置分页信息
        List<FitnessActionsModel> ActionList =fitnessActionMapper.queryByTypes(pageRequest,venueUniqueIdentifier,fitnessPurposeId,fitnessTypeId);
        for(FitnessActionsModel Model:ActionList){
            Integer id = Model.getId();
            Model.setFitnessActionCountAll(fitnessPlanActionGroupMapper.selectGroupAll(id));
        }
        return MybatisPageHelper.getPageResult(ActionList);
    }

    /**
     * @Author 杨珅
     * @Description todo 根据健身目的查询健身标签
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    public CommonResult selectTypeAll(PageRequest pageRequest,Integer id) {
        return CommonResult.ok(fitnessActionMapper.selectTypeAll(pageRequest,id));
    }
    /**
     * @Author 杨珅
     * @Description todo 查询健身目的
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    public CommonResult selectPurposeAll(String venueUniqueIdentifier) {
        return CommonResult.ok(fitnessActionMapper.selectPurposeAll(venueUniqueIdentifier));
    }
    /**
     * @Author 杨珅
     * @Description todo 查询健身器材
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    public CommonResult selectEquipmentAll(String venueUniqueIdentifier) {
        return CommonResult.ok(fitnessActionMapper.selectEquipmentAll(venueUniqueIdentifier));
    }

    /**
     * @Author 杨珅
     * @Description todo 新增健身动作
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    public CommonResult insertFitnessAction(FitnessActionModel fitnessAction)throws CommonException {
        if (ValidatorUtil.ValidationObject(fitnessAction)){
        fitnessAction.setCreateTime(new Date());
        fitnessAction.setUpdateTime(new Date());
            int i = fitnessActionMapper.insertFitnessAction(fitnessAction);
            if (i >= 1) {
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.SAVE_ERROR);
            }
        } else{
            return CommonResult.ok(EmErrorCode.SAVE_ERROR);
        }
    }
    /**
     * @Author 杨珅
     * @Description todo 根据健身目的查询健身标签
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    public CommonResult insertImg(MultipartFile file) {
        String fileId = UploadFileUtils.uploadAvatar(file);
        return CommonResult.ok(fileId);
    }
    /**
     * @Author 杨珅
     * @Description todo 修改健身动作
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    @Transactional
    public CommonResult updateFitnessAction(FitnessActionModel fitnessAction)throws CommonException {
        if (ValidatorUtil.ValidationObject(fitnessAction)){
        fitnessAction.setEquipmentId(fitnessAction.getEquipmentId());
        fitnessAction.setFitnessPurposeId(fitnessAction.getFitnessPurposeId());
            int i = fitnessActionMapper.updateFitnessAction(fitnessAction);
            if (i >= 1) {
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
            }
        } else{
            return CommonResult.ok(EmErrorCode.UPDATE_ERROR);
        }
    }
    /**
     * @Author 杨珅
     * @Description todo 逻辑删除健身动作
     * @Date 2023/11/13 8:43
     * @Return  * @param null
     */
    @Override
    public CommonResult deleteFitnessAction(FitnessActionModel fitnessAction) {
        return CommonResult.ok(fitnessActionMapper.deleteFitnessAction(fitnessAction));
    }

    @Override
    public CommonResult selectAlll() {
        return CommonResult.ok(fitnessActionMapper.queryAlll());
    }
}
